﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace TriplesGameDAL
{
    public static class TriplesGameDAO
    {
        public const string WAITING_FOR_PLAYER_TO_JOIN = "Waiting for player to join game.";
        public const string PLAYER_A_TURN = "Player A is next.";
        public const string PLAYER_B_TURN = "Player B is next.";

        public const string PLAYER_A_NO_MORE_TURNS = "Player A cannot make a move.";
        public const string PLAYER_B_NO_MORE_TURNS = "Player B cannot make a move.";

        public const string GAME_OVER = "Game over.";

        public const string PLAYER_A_CHAR = "A";
        public const string PLAYER_B_CHAR = "B";

        public static Player CreatePlayer()
        {
            Player player = new Player();

            using (TriplesGameEntities context = new TriplesGameEntities())
            {
                Game game = context.Games.FirstOrDefault(g => g.State == WAITING_FOR_PLAYER_TO_JOIN);

                if (game == null)
                {
                    player.PlayerChar = PLAYER_A_CHAR;
                    game = new Game();
                    game.Matrix = new string(' ', 50);
                    game.Players.Add(player);
                    game.State = WAITING_FOR_PLAYER_TO_JOIN;

                    context.Players.AddObject(player);
                    context.Games.AddObject(game);
                }
                else
                {
                    game.State = PLAYER_A_TURN;
                    player.GameId = game.GameId;
                    player.PlayerChar = PLAYER_B_CHAR;
                    context.Players.AddObject(player);                    
                }

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    return CreatePlayer();
                }
            }

            return player;
        }

        public static Game GetGameByPlayerId(int playerId)
        {
            using (TriplesGameEntities context = new TriplesGameEntities())
            {
                return context.Players.First(p=> p.PlayerId == playerId).Game;
            }
        }

        public static void SaveGame(Game game)
        {
            using (TriplesGameEntities context = new TriplesGameEntities())
            {
                context.Games.Attach(game);

                context.ObjectStateManager.GetObjectStateEntry(game).SetModified();
                context.Refresh(RefreshMode.ClientWins, game);

                context.SaveChanges(); 
            }
        }

        public static char GetPlayerCharByPlayerId(int playerId)
        {
            using (TriplesGameEntities context = new TriplesGameEntities())
            {
                return context.Players.First(p => p.PlayerId == playerId).PlayerChar[0];
            }
        }
    }
}